package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.domain.AdminSmMenu;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmAuthRecoMapper;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmMenuMapper;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @项目名称: yusp-admin
 * @类名称: AdminMenuConfigService
 * @类描述: 菜单配置服务层
 * @功能描述:
 * @创建人: weimei1@yusys.com.cn
 * @创建时间: 2017-12-23 10:25
 * @修改备注:
 * @修改记录: 修改时间 修改人员 修改原因
 *        -------------------------------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@Service
public class AdminSmMenuService extends CommonService {

    @Autowired
    private AdminSmMenuMapper adminSmMenuMapper;

    @Autowired
    private AdminSmAuthRecoMapper adminSmAuthRecoMapper;

    @Override
    protected CommonMapper<?> getMapper() {
        return this.adminSmMenuMapper;
    }

    /**
     * @方法名称:getMenuTree
     * @方法描述:菜单树初始化查询
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getMenuTree(String sysId,boolean lazy,String menuId) {
    	List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
    	if(lazy) {
    		Map<String, String> paramMap=new HashMap<String, String>();
			paramMap.put("sysId", sysId);
			paramMap.put("menuId", menuId);
    		list = adminSmMenuMapper.getMenuTreeLazy(paramMap);
    	}else {
    		Map<String, Object> root=new HashMap<String, Object>();
    		root.put("menuId","0");
    		list = adminSmMenuMapper.getMenuTree(sysId);
    		list.add(root);
    	}
        return list;
    }

    /**
     * @方法名称:getMenuInfo
     * @方法描述:菜单节点信息查询
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = true)
    public Map<String, Object> getMenuInfo(String menuId) {
        return adminSmMenuMapper.getMenuInfo(menuId);
    }

    /**
     * @方法名称: createMenu
     * @方法描述: 新增菜单
     * @参数与返回说明:
     * @算法描述:
     */
    public int createMenu(AdminSmMenu adminSmMenu) {
        adminSmMenu.setLastChgDt(DateUtil.formatDateTimeByDef());
        return this.insertSelective(adminSmMenuMapper, adminSmMenu);
    }

    /**
     * @方法名称: editMenu
     * @方法描述: 修改菜单
     * @参数与返回说明:
     * @算法描述:
     */
    public int editMenu(AdminSmMenu adminSmMenu) {
        adminSmMenu.setLastChgDt(DateUtil.formatDateTimeByDef());
        return this.updateSelective(adminSmMenuMapper, adminSmMenu);
    }

    /**
     * @方法名称:deleteMenu
     * @方法描述: 删除菜单，同时删除菜单子节点及菜单引用控制点、数据权限的对应授权数据
     * @参数与返回说明:
     * @算法描述:
     */
    @SuppressWarnings("rawtypes")
    @Transactional(readOnly = false, rollbackFor = { Exception.class, RuntimeException.class })
    public int deleteMenu(String menuId) {
        ObjectMapper mapper = new ObjectMapper();
        String id =null;
        try {
            Map idMap = mapper.readValue(menuId, Map.class);
            id = (String) idMap.get("menuId");
        } catch (IOException e) {
            e.printStackTrace();
        }
        int n = 0;
        List<String> list = adminSmMenuMapper.getDeleteMenuId(id);
        for (int i = 0, listLen = list.size(); i < listLen; i++) {
            this.adminSmAuthRecoMapper.deleteMenuInfo(list.get(i));
            n = n + super.deleteByPrimaryKey(list.get(i));
        }
        return n;
    }

    /**
     * @方法名称: getFuncInfo
     * @方法描述: 业务功能列表查询
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getFuncInfo(QueryModel model) {
        PageHelper.startPage(model.getPage(), model.getSize());
        if (model.getCondition().containsKey("queryKey")) {
            model.getCondition().put("queryKey", "%" + model.getCondition().get("queryKey") + "%");
        }
        List<Map<String, Object>> list = adminSmMenuMapper.getFuncInfo(model);
        PageHelper.clearPage();
        return list;
    }
}
